package cn.explink.dop.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.explink.dop.dao.DeliveryCompanyDao;
import cn.explink.dop.dao.DeliveryManDao;
import cn.explink.dop.entity.vo.DeliveryManVO;
import cn.explink.dop.service.DeliveryManService;
import cn.explink.eap.core.common.service.impl.CommonServiceImpl;

/**
 * 业务层
 *
 * @author firefly 2015-2-28 11:04
 *
 */
@Service("deliveryManService")
public class DeliveryManServiceImpl extends CommonServiceImpl implements DeliveryManService {

	@Autowired
	private DeliveryManDao deliveryManDao;
	
	@Autowired
	private DeliveryCompanyDao deliveryCompanyDao; 
	@Override
	public String add(final DeliveryManVO del) {
		return (String) this.deliveryManDao.save(del);
	}

	@Override
	public void modify(final DeliveryManVO del) {
		this.deliveryManDao.modify(del);
	}

	@Override
	public List<DeliveryManVO> getListData(final DeliveryManVO del) {
		List<String> list = deliveryCompanyDao.queryValiableCompanyId();
		return this.deliveryManDao.finds(del,list);
	}

	@Override
	public Map<String, Object> getListDataByPage(final DeliveryManVO del, final int pageNum, final int pageSize) {
		return this.deliveryManDao.findByPage(del, pageNum, pageSize);
	}

	@Override
	public DeliveryManVO queryById(String deliveryManId) {
		DeliveryManVO deliveryManVO = this.deliveryManDao.queryById(deliveryManId);
		return deliveryManVO;
	}

	@Override
	public int deleteData(final List<DeliveryManVO> delList) {
		return this.deliveryManDao.delete(delList);
	}

	@Override
	public List<Map<String, Object>> userList() {
		return this.deliveryManDao.getUserList();
	}

	/**
	 *
	 * @Title: getUsersAndSelf
	 * @Description: 根据用户ID来查询信息
	 * @author 孟凡岭
	 * @param @param userId
	 * @param @return
	 * @throws
	 */
	@Override
	public Map<String, Object> getUsersAndSelf(String userId) {
		// TODO Auto-generated method stub
		return this.deliveryManDao.getUsersAndSelf(userId);
	}

	@Override
	public DeliveryManVO queryByUserId(String userId) {
		return this.deliveryManDao.queryByUserId(userId);
	}

	/**
	 *
	 * @Title: checkUserRelevance
	 * @Description: 判断用户是否已经有关联
	 * @author 孟凡岭
	 * @param @param userId
	 * @param @return
	 * @throws
	 */
	@Override
	public boolean checkUserRelevance(String userId) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> rs = this.deliveryManDao.checkUserRelevance(userId);
		/**
		 * 如果list的size大于0说明已经有关联
		 */
		if (rs.size() > 0) {
			return false;
		}
		return true;
	}

	/**
	 *
	 * @Title: chekcDelCertificates
	 * @Description: 判断证件编号是否已经存在
	 * @author 孟凡岭
	 * @param @param delCertificates
	 * @param @return
	 * @throws
	 */
	@Override
	public boolean chekcDelCertificates(String delCertificates) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> result = this.deliveryManDao.chekcDelCertificates(delCertificates);
		return result.size() == 0;
	}

	/**
	 *
	 * @Title: getHeadImg
	 * @Description: 获取人员头像
	 * @author 孟凡岭
	 * @param @param Id
	 * @param @return
	 * @throws
	 */
	@Override
	public String getHeadImg(String Id) {
		// TODO Auto-generated method stub
		DeliveryManVO vo = this.deliveryManDao.queryById(Id);
		/**
		 * 如果已经上传头像，返回头像地址，否则返回默认设置图片
		 */
		if ((vo.getDelPortrait() != null) && StringUtils.isNotBlank(vo.getDelPortrait())) {
			return vo.getDelPortrait();
		}
		return "/upload/defaultHeadImg.jpg";
	}

	/**
	 *
	 * @Title: updateStatus
	 * @Description: 更改人员状态和用户状态
	 * @author 孟凡岭
	 * @param @param ids
	 * @return void 返回类型
	 * @throws
	 */
	@Override
	public void updateStatus(List<String> ids, int status) {
		// TODO Auto-generated method stub
		this.deliveryManDao.updateStatus(ids, status);
		if (status == 2) {
			this.deliveryManDao.updateUser(ids, 0);
		} else {
			this.deliveryManDao.updateUser(ids, 1);
		}
	}

	/**
	 *
	 * @Title: getOpenDeliveryMan
	 * @Description: 获取启用中的配送人员
	 * @author 孟凡岭
	 * @param @return
	 * @throws
	 */
	@Override
	public List<Map<String, Object>> getOpenDeliveryMan() {
		// TODO Auto-generated method stub
		return this.deliveryManDao.getOpenDeliveryMan();
	}

	/**
	 *
	 * @Title: setUserToNullByUserId
	 * @Description: 删除配送员，逻辑删除
	 * @author 孟凡岭
	 * @param @param id
	 * @throws
	 */
	@Override
	public void deleteByUserId(String id) {
		// TODO Auto-generated method stub
		this.deliveryManDao.deleteByUserId(id);
	}

	/**
	 *
	 * @Title: getOpenAndReceiveDeliveryMan
	 * @Description: 获取启用中并且可以接单的配送人员
	 * @author 孟凡岭
	 * @param @return
	 * @throws
	 */
	@Override
	public List<Map<String, Object>> getOpenAndReceiveDeliveryMan() {
		// TODO Auto-generated method stub
		return this.deliveryManDao.getOpenAndReceiveDeliveryMan();
	}

	/**
	 *
	 * @Title: queryManListPageByCompanyId
	 * @Description: 通过公司号查询配送员信息
	 * @author 孟凡岭
	 * @param @param man
	 * @param @param companyId
	 * @param @param pageNum
	 * @param @param pageSize
	 * @param @return
	 * @throws
	 */
	@Override
	public Map<String, Object> queryManListPageByCompanyId(DeliveryManVO man, String companyId, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		return this.deliveryManDao.queryManListPageByCompanyId(man, companyId, pageNum, pageSize);
	}
}
